<template>
  <div class="app-container">
    <el-container>
      <el-aside width="50%">
        <h4>商品选择列表</h4>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="关键字查询" prop="mark" label-width="90px">
            <el-input
              v-model="queryParams.mark"
              placeholder="请输入关键字查询"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['inventory:check:add']"
            >添加</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="商品名称" align="center" prop="goodsName" />
          <el-table-column label="商品规格" align="center" prop="specificationName" />
          <el-table-column label="销售价" align="center" prop="price" />
          <el-table-column label="成本价" align="center" prop="oldPrice" />
          <el-table-column label="库存" align="center" prop="stock" />
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-aside>
      <el-main>
        <h4>已选商品</h4>
        <el-table v-loading="loading" :data="getGoodsList">
          <el-table-column label="商品名称" align="center" prop="goodsName" />
          <el-table-column label="商品规格" align="center" prop="specificationName" />
          <el-table-column label="成本价" align="center" prop="oldPrice" />
          <el-table-column label="库存" align="center" prop="stock" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                    >删除</el-button>
                  </template>
          </el-table-column>
        </el-table>
        <el-button
          type="primary"
          plain
          icon="el-icon-check"
          size="mini"
          @click="baocun"
          v-hasPermi="['inventory:check:add']"
        >保存</el-button>
        <el-button @click="close()">返回</el-button>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import {getGoods, updateStatus,} from "@/api/inventory/check";
import store from "@/store";
import router from "@/router";
export default {
  name: "getGoods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 选中数组
      xuan: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mark: null
      },
      // 商品集合
      goodsList: [],
      // 已选商品集合
      getGoodsList: [],
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.xuan = selection.map(item => item)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 添加按钮
    handleAdd() {
      this.getGoodsList = this.xuan;
      console.log(this.getGoodsList);
    },
    // 得到商品列表
    getList() {
      this.loading = true;
      getGoods({mark: this.queryParams.mark, warehouseCode: this.$route.query.warehouseCode}).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 删除
    handleDelete(row) {
      for (let i = 0; i < this.getGoodsList.length; i++) {
        if (row === this.getGoodsList[i]) {
          this.getGoodsList.splice(i,1);
        }
      }
    },
    // 保存
    baocun() {
      if (this.getGoodsList.length === 0) {
        this.$modal.msgError("请先添加商品再保存");
        return;
      }
      store.dispatch("tagsView/delView", router.currentRoute);
      this.$router.push({path: "/check/add/addCheck", query: {getGoodsList: this.getGoodsList}});
    },
    close() {
      const obj = { path: "/check/add/addCheck" };
      this.$tab.closeOpenPage(obj);
    },
  }
}
</script>

<style scoped>

</style>
